<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="围栏名称" prop="name" :rules="[
                  {required: true, message:'围栏名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写围栏名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警开关" prop="state" :rules="[
                  {required: true, message:'报警开关不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.state" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('switch')" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警方式" prop="type" :rules="[
                  {required: true, message:'报警方式不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.type" placeholder="请选择报警方式" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('lora_fence_alarm')" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="触发区域" prop="areaType" :rules="[
                  {required: true, message:'触发区域不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.areaType" placeholder="请选择" style="width: 100%;" @change="inputForm.areaInfo=''">
                <el-option v-for="item in $dictUtils.getDictList('lora_fence_area')" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="inputForm.areaType ==='整栋建筑'">
            <el-form-item label="选择建筑" prop="areaInfo" :rules="[
              {required: true, message:'建筑不能为空', trigger:'blur'}
              ]">
              <el-select v-model="inputForm.areaInfo" placeholder="请选择建筑" style="width: 100%;">
                <el-option v-for="item in buildingList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-else-if="inputForm.areaType ==='单个楼层'">
            <el-form-item label="选择楼层" prop="areaInfo"
                :rules="[
                  {required: true, message:'楼层不能为空', trigger:'blur'}
                 ]">
              <SelectTree
                ref="floor"
                placeholder="请选择楼层"
                :props="{
                    value: 'id',             // ID字段名
                    label: 'showName',         // 显示名称
                    children: 'childNodes'    // 子级字段名
                  }"
                url="/smis/floor/jfFloor/treeData"
                :value="inputForm.areaInfo"
                :clearable="true"
                :accordion="true"
                @getValue="(val)=>{inputForm.areaInfo = val||'';}"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-else-if="inputForm.areaType ==='自定义区域'">
            <el-form-item label="选择区域" prop="areaInfo"
                :rules="[
                  {required: true, message:'区域不能为空', trigger:'blur'}
                 ]">
                 <div class="block">
                   <CasCader
                     style="width:100%"
                     url = "/lora/area/jfLoraArea/treeData"
                     :value="inputForm.areaInfo"
                     :clearable="true"
                     :accordion="true"
                     :props="{ multiple: true, checkStrictly: true, value: 'id', label: 'name', children: 'childNodes'}"
                     placeholder="请选择区域"
                     @getValue="(value) => {inputForm.areaInfo=value}"/>
                 </div>
              <!-- <SelectTree
                ref="area"
                placeholder="请选择区域"
                :showCheckbox="true"
                :props="{
                    value: 'id',             // ID字段名
                    label: 'name',         // 显示名称
                    children: 'childNodes'    // 子级字段名
                  }"
                url="/lora/area/jfLoraArea/treeData"
                :value="inputForm.areaInfo"
                :clearable="true"
                :accordion="false"/> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="触发对象" prop="departIds"
              :rules="[
                {required: true, message:'触发对象不能为空', trigger:'blur'}
              ]">
              <div class="block">
                <CasCader
                  style="width:100%"
                  url = "/lora/user/jfLoraDepart/treeData"
                  :value="inputForm.departIds"
                  :clearable="true"
                  :accordion="true"
                  :extend = "departExtend"
                  placeholder="请选择触发部门"
                  @getValue="(value) => {inputForm.departIds=value}"/>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="报警时间段" prop="timeRange" :rules="[
              {required: true, message:'报警时间段不能存在空数据', trigger:'blur'}
            ]">
              <el-button type="success" icon="el-icon-plus" circle @click="addTimeRange"
                v-if="timeRangeList.length === 0"></el-button>
              <div v-for="(item,index) in timeRangeList">
                <el-col :span="18">
                  <el-form-item label="" prop="time" :rules="[
                  ]">
                    <el-time-picker is-range v-model="timeRangeList[index]" range-separator="至"
                      start-placeholder="开始时间" value-format="HH:mm:ss" end-placeholder="结束时间" placeholder="选择时间范围">
                    </el-time-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6" >
                  <el-button type="danger" icon="el-icon-minus" circle @click="deleteTimeRange(index)"
                    style="margin: 3px;"></el-button>
                  <el-button type="success" icon="el-icon-plus" circle
                    v-if="index === timeRangeList.length-1" @click="addTimeRange" style="margin: 3px;">
                  </el-button>
                </el-col>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import CasCader from '@/components/cascader'
  export default {
    data() {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        timeRange: ['00:08:00', '22:00:00'],
        timeRangeList: [],
        inputForm: {
          id: '',
          name: '',
          type: '',
          state: '',
          areaType: '',
          areaInfo: '',
          timeRange: '',
          departIds: ''
        },
        departExtend: [{
          id: 'fangke',
          name: '访客'
        },{
          id: 'waibaojiediao',
          name: '外包借调'
        },{
          id: 'truck',
          name: '外来车辆'
        }]
      }
    },
    components: {
      SelectTree,
      CasCader
    },
    computed: {
      buildingList: {
        get() {
          return this.$store.state.shopdata.buildingList
        }
      }
    },
    methods: {
      init(method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建围栏`
        } else if (method === 'edit') {
          this.title = '修改围栏'
        } else if (method === 'view') {
          this.title = '查看围栏'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/lora/fence/jfLoraFence/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({
              data
            }) => {
              this.inputForm = this.recover(this.inputForm, data.jfLoraFence)
              this.timeRangeList = JSON.parse(this.inputForm.timeRange || '[]')
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit() {
        this.inputForm.timeRange = JSON.stringify(this.timeRangeList)
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/lora/fence/jfLoraFence/save`,
              method: 'post',
              data: this.inputForm
            }).then(({
              data
            }) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      addTimeRange() {
        this.timeRangeList.push(['00:08:00', '22:00:00'])
      },
      deleteTimeRange(index) {
        this.timeRangeList.splice(index, 1);
      },
    }
  }
</script>
<style scoped="scoped">
  .uni-notice {
    margin-left: 120px;
    padding-bottom: 30px;
  }
</style>
